
$('#addIsvPage').on('hidden.bs.modal', function () {
    $("#isvPid").val('');
    $("#isvName").val('');
});

$('#addDeptPage').on('hidden.bs.modal', function () {
    $("#deptCode").val('');
    $("#deptName").val('');
});

$('#addCityPage').on('hidden.bs.modal', function () {
    $('#_searchKey').val('');
    newCheck.setUnCheckedByName('cityAll');
    newCheck.setUnCheckedByName('cityCb');
    newCheck.setUnCheckedByName('cityInverse');
});

$(function () {
    if (editType != 'info') {
        regValidate(["#_outputDataForm"]);

        initTextSearchWithParam("#isvName",
            basePath + 'output/chooseIsvData?t='+new Date().getTime(),
            ['isvPid', 'isvName'],
            {'isvPid':'服务商PID', 'isvName':'服务商名称'},
            'isvPid',
            'isvName',
            true,
            selectIsvValue,
            noSelectIsvValue,
            getIsvDataParam);

        initTextSearchWithParam("#deptName",
            basePath + 'output/getDeptData?t='+new Date().getTime(),
            ['deptCode', 'deptName'],
            {'deptCode':'科室CODE', 'deptName':'科室名称'},
            'deptCode',
            'deptName',
            true,
            selectDeptValue,
            noSelectDeptValue,
            getDeptDataParam);

        newCheck.initByName('cityAll');
        newCheck.initByName('cityInverse');
        newCheck.initByName('cityCb');
        newCheck.addEventByName('cityAll', setNoCheckCity, 'ifChecked');
        newCheck.addEventByName('cityInverse', inverseCity, 'ifClicked');
        newCheck.addEventByName('cityCb', setNoCheckAll, 'ifChecked');
    }
});
//全国事件--所有城市取消选择
function setNoCheckCity() {
    newCheck.setUnCheckedByName('cityCb');
}
//城市时间-全国取消选择
function setNoCheckAll() {
    newCheck.setUnCheckedByName('cityAll');
}

//反选事件
function inverseCity() {
    newCheck.setUnCheckedByName('cityAll');
    var checkedItems = $('input[type=checkbox][name="cityCb"]:checked');
    var unCheckedItems = $('input[type=checkbox][name="cityCb"]:not(:checked)');
    checkedItems.iCheck('uncheck');
    unCheckedItems.iCheck('check');
}

function getIsvDataParam() {
    var isvName = $('#isvName').val();
    var isvPids = getLabelNoteStr('_isvTags', false);
    var bizCode = $('#pBizCode').val();
    return {
        isvPids: isvPids,
        isvName: isvName,
        bizCode: bizCode,
    };
}

function getDeptDataParam() {
    var deptName = $('#deptName').val();
    var deptCodes = getLabelNoteStr('_deptTags', false);
    return {
        deptName : deptName,
        deptCodes : deptCodes,
    };
}

function selectIsvValue(data) {
    if (!data || !data.isvPid) {
        return;
    }
    $('#isvPid').val(data.isvPid);
}

function noSelectIsvValue() {
    $('#isvPid').val('');
}

function selectDeptValue(data) {
    if (!data || !data.deptCode) {
        return;
    }
    $('#deptCode').val(data.deptCode);
}

function noSelectDeptValue() {
    $('#deptCode').val('');
}

function doChangeParentBiz() {
    clearIsv();
    $('#bizCode').val('')
    var pBizCode = $('#pBizCode').val();
    var strHtml = '<option value="">--请选择--</option>';
    if (!pBizCode || pBizCode == '') {
        $('#bizCode').html(strHtml);
    } else {
        var postData = {
            bizParentCode : pBizCode,
        };
        $.ajax({
            type: 'post',
            url: basePath + 'output/selectBiz?t=' + new Date().getTime(),
            dataType: 'json',
            data: postData,
            success : function (data) {
                if(data && data.result){
                    setBizSelect(data.content);
                }else if(data&&!data.result){
                    toastr.error(data.message);
                }else{
                    toastr.error('获取业务服务项信息失败');
                }
            },
            error: function (request) {
                delayToastr(common_system_error);
            }
        });
    }
}

function setBizSelect(bizList) {
    var htmlStr = '<option value="">--请选择--</option>';
    if (bizList) {
        for (var i=0; i<bizList.length; i++) {
            htmlStr += '<option value="'+bizList[i].code+'">'+bizList[i].name+'</option>';
        }
    }
    $('#bizCode').html(htmlStr);
}

function doSave() {
    if(!$("#_outputDataForm").valid()) return;//校验不通过
    if(!checkOutputData()) return;
    var destType = $('#destType').val();
    var bizCode = $('#bizCode').val();
    var isvPids = getLabelNoteStr('_isvTags', false);
    var cityCodes = getLabelNoteStr('_cityTags', false);
    var deptCodes = getLabelNoteStr('_deptTags', false);
    var sortNum = $('#sortNum').val();
    var remark = $('#remark').val();
    sortNum = sortNum ? sortNum : 999;
    var postData = {
        bizCode : bizCode,
        isvPids : isvPids == 'all' ? '' : isvPids,
        deptCodes : deptCodes == 'all' ? '' : deptCodes,
        cityCodes : cityCodes,
        sortNum : sortNum,
        remark : remark,
        destType : destType,
    };
    var url = basePath + 'output/addOutput';
    if (editType == 'edit') {
        postData.id = oneOutputInfo.id;
        url = basePath + 'output/editOutput';
    }
    $.ajax({
        type: 'post',
        url: url,
        dataType: 'json',
        data: JSON.parse(JSON.stringify(postData)),
        success : function (data) {
            if (!data) {
                toastr.error(common_save_fail);
                return;
            }
            if (data.result) {
                doBack();
                return;
            }
            if (!data.result) {
                toastr.error(data.errorDesc);
                return;
            }
        },
        error: function (request) {
            toastr.error(common_system_error);
            return;
        }
    });
}

function string2List(str) {
    if (!str || str == '') {
        return [];
    }
    return str.split(',');
}

function checkOutputData() {
    var isvPids = getLabelNoteStr('_isvTags', false);
    var cityCodes = getLabelNoteStr('_cityTags', false);
    var deptCodes = getLabelNoteStr('_deptTags', false);
    if (!isvPids || isvPids == '') {
        toastr.error('业务主页信息不全，服务商不能为空');
        return false;
    }
    if (!cityCodes || cityCodes == '') {
        toastr.error('业务主页信息不全，城市不能为空');
        return false;
    }
    if (!deptCodes || deptCodes == '') {
        toastr.error('业务主页信息不全，关联一级科室不能为空');
        return false;
    }
    return true;
}

function doCheckDataChange() {
    var bizCode = $('#bizCode').val();
    var sortNum = $('#sortNum').val();
    var isvPids = getLabelNoteStr('_isvTags', false);
    var cityCodes = getLabelNoteStr('_cityTags', false);
    var deptCodes = getLabelNoteStr('_deptTags', false);
    var remark = $('#remark').val();

    var checkData = {
        bizCode : bizCode,
        sortNum : sortNum,
        isvPids : isvPids,
        cityCodes : cityCodes,
        deptCodes : deptCodes,
        remark : remark,
    };
    if (editType != 'info' && !Compare(checkData, oneOutputInfo)) {
        swalClosePageToastr(doBack);
        return;
    }
    doBack();
}

function doBack() {
    var str = searchStr.replace(/,/g, '&');
    window.location.href = basePath + 'output/outputPage?t=' + new Date().getTime() + '&' + str;
}

function openAddIsvPage() {
    $('#addIsvPage').modal('show');
}

function doAddIsv() {
    var isvPid = $('#isvPid').val();
    var isvName = $('#isvName').val();
    if (!isvPid || isvPid == '' || !isvName || isvName == '') {
        toastr.error('请先选择服务商再保存');
        return;
    }
    var tagList = [{
        value : isvPid,
        text : isvName
    }];
    $('#addIsvPage').modal('hide');
    var isvPids = getLabelNoteStr('_isvTags', false);
    if (isvPids == 'all') {
        htmlLableByList(tagList, "_isvTags");
    } else {
        addLableByList(tagList, "_isvTags");
    }
}

function openAddDeptPage() {
    $('#addDeptPage').modal('show');
}

function doAddDept() {
    var deptCode = $('#deptCode').val();
    var deptName = $('#deptName').val();
    if (!deptCode || deptCode == '' || !deptName || deptName == '') {
        toastr.error('请先选择科室再保存');
        return;
    }
    var tagList = [{
        value : deptCode,
        text : deptName
    }];
    $('#addDeptPage').modal('hide');
    var deptCodes = getLabelNoteStr('_deptTags', false);
    if (deptCodes == 'all') {
        htmlLableByList(tagList, "_deptTags");
    } else {
        addLableByList(tagList, "_deptTags");
    }
}

function openAddCityPage() {
    var cityCodes = getLabelNoteStr('_cityTags', false);
    if (cityCodes == 'all') {
        newCheck.setCheckedByName('cityAll');
    } else {
        var cityCodeList = cityCodes.split(',');
        $.each(cityCodeList,function(n,value) {
            newCheck.setCheckedByNameAndValue('cityCb', value);
        });
    }
    $('#addCityPage').modal('show');
}

function queryCity() {
    var name = $.trim($("#_searchKey").val());
    if(!name){
        $("#myCities label").show();
        return false;
    }
    //全部隐藏
    $("#myCities label").hide();
    $("label[idd*="+ name +"]").show();
}


function doAddCity() {
    var cityCodes= $('input[type=checkbox][name="cityCb"]:checked');
    var allCode = $('input[type=checkbox][name="cityAll"]:checked');
    if ((!cityCodes || cityCodes.length == 0) && (!allCode || allCode.length == 0)) {
        toastr.error('请选择指定城市');
        return;
    }
    var tagList = [];
    $.each(cityCodes,function (index,val) {
        tagList.push({
            value : $(this).val(),
            text : $(this.parentElement).parent().attr('idd'),
        });
    });
    $.each(allCode,function (index,val) {
        tagList.push({
            value : $(this).val(),
            text : $(this.parentElement).parent().attr('idd'),
        });
    });
    $('#addCityPage').modal('hide');
    htmlLableByList(tagList, "_cityTags");
}

function clearIsv() {
    var tagList = [{
        value : 'all',
        text : '全部'
    }];
    htmlLableByList(tagList, "_isvTags");
}

function clearDept() {
    var tagList = [{
        value : 'all',
        text : '全部'
    }];
    htmlLableByList(tagList, "_deptTags");
}

function clearCity() {
    var tagList = [{
        value : 'all',
        text : '全国'
    }];
    htmlLableByList(tagList, "_cityTags");
}


